package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.DepartmentMapper;
import com.xxxx.server.pojo.Department;
import com.xxxx.server.common.resp.RespBean;
import com.xxxx.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2021-12-20
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     添加部门
     */
    public RespBean addDepartment(Department department){
        // 默认启用
        department.setEnabled(true);
        departmentMapper.addDepartment(department);
        if (department.getResult() == 1) {
            return RespBean.success("添加部门成功！");
        } else {
            return RespBean.error("添加部门失败！");
        }
    }

    /**
     * 删除部门
     * @param depId
     * @return
     */
    public RespBean deleteDepartment(Integer depId){
        Department department = new Department();
        department.setId(depId);
        departmentMapper.deleteDepartment(department);
        if (department.getResult() == -2) {
            return RespBean.error("该部门下还有子部门，删除失败！");
        }
        if (department.getResult() == -1) {
            return RespBean.error("该部门下还有员工，删除失败！");
        }
        if (department.getResult() == 1) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }


    /**
     查询所有部门
     */
    public List<Department> getAllDepartment(){
        return departmentMapper.getAllDepartment(-1);
    }
}
